package com.tonghe.controller;

import com.tonghe.annotation.SystemLog;
import com.tonghe.domain.ResponseResult;
import com.tonghe.domain.dto.AddUserDTO;
import com.tonghe.domain.entity.User;
import com.tonghe.enums.AppHttpCodeEnum;
import com.tonghe.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Email;

/**
 * @Date:2022/3/14- 03- 14- 19:59
 * @author: tonghe
 **/
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
    * @description: 获取用户信息
    * @return： com.tonghe.domain.ResponseResult
    * @author: TongHe
    */
    @SystemLog(businessName = "获取用户信息")
    @GetMapping("/userInfo")
    public ResponseResult UserInfo(){
        return userService.userInfo();
    }

    /**
    * @description: 更新用户信息
    * @param： [user]
    * @return： com.tonghe.domain.ResponseResult
    * @author: TongHe
    */
    @PutMapping("/userInfo")
    @SystemLog(businessName = "修改个人信息")
    public ResponseResult updateUserInfo(@RequestBody User user){
        return userService.updateUserInfo(user);
    }

    /**
     * @description: 用户注册
     * @author: TongHe
     */
    @PostMapping("/register")
    @SystemLog(businessName = "用户注册")
    public ResponseResult register(@RequestBody AddUserDTO userDTO){
        return userService.register(userDTO);
    }

    /**
     * @description: 获取验证码
     * @author: TongHe
     */
    @SystemLog(businessName = "发送验证码")
    @GetMapping("/msgcode")
    public ResponseResult msgCode(@Email  String mailAddress) {
        if(!StringUtils.hasText(mailAddress)){
            return ResponseResult.errorResult(AppHttpCodeEnum.EMAIL_NOT_NULL);
        }
        return userService.getMsgCode(mailAddress);
    }
}
